package cn.dslcode.spring.session.redis.config;

import cn.dslcode.common.core.string.StringUtil;
import cn.dslcode.common.web.CookieUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.session.Session;
import org.springframework.session.web.http.HttpSessionStrategy;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Created by dongsilin on 2018/1/18.
 */
@Slf4j
@Component
public class CommonHttpSessionStrategy implements HttpSessionStrategy {

//    @Qualifier("sessionRedisTemplate")
//    @Autowired
//    private RedisTemplate redisTemplate;

    @Value("${spring.session.recognize.name}")
    private String sessionRecognizeName;

    protected volatile int sessionMaxAge;

//    ThreadLocal<Boolean> threadLocal = new ThreadLocal();

    @Override
    public String getRequestedSessionId(HttpServletRequest request) {
        String sessionId = request.getHeader(sessionRecognizeName);
        if (StringUtil.isEmpty(sessionId)) sessionId = CookieUtil.getValue(request, sessionRecognizeName);
        return sessionId;
    }

    @Override
    public void onNewSession(Session session, HttpServletRequest request, HttpServletResponse response) {
//        Boolean isExecute = threadLocal.get();
//        if (isExecute == null){
//            threadLocal.set(Boolean.TRUE);
//            String headerSessionId = request.getHeader(sessionRecognizeName);
//            String springSessionId = session.getId();
//            // 使用自带的id
//            if (StringUtil.isNotEmpty(headerSessionId)) {
//                log.debug(headerSessionId);
//                renameKeyName(springSessionId, headerSessionId);
//                addResponse(headerSessionId, response);
//            } else addResponse(springSessionId, response);
//        }
        addResponse(session.getId(), response);
    }

    @Override
    public void onInvalidateSession(HttpServletRequest request, HttpServletResponse response) {
        addResponse("", response);
    }

//    /**
//     * redis key重命名
//     * @param springSessionId
//     * @param headerSessionId
//     */
//    private void renameKeyName(String springSessionId, String headerSessionId){
//        List<String> keys = CollectionUtil.getList(
//                "spring:session:sessions:".concat(springSessionId), "spring:session:sessions:".concat(headerSessionId),
//                "spring:session:sessions:expires:".concat(springSessionId), "spring:session:sessions:expires:".concat(headerSessionId)
//        );
//        redisTemplate.execute(new DefaultRedisScript<Void>("redis.call('RENAME', KEYS[1], KEYS[2]);redis.call('RENAME', KEYS[3], KEYS[4]);", Void.class), keys, Boolean.TRUE);
////                redisTemplate.renameIfAbsent("spring:session:sessions:".concat(springSessionId), "spring:session:sessions:".concat(headerSessionId));
////                redisTemplate.renameIfAbsent("spring:session:sessions:expires:".concat(springSessionId), "spring:session:sessions:expires:".concat(headerSessionId));
//    }

    private void addResponse(String value, HttpServletResponse response){
        response.setHeader(sessionRecognizeName, value);
        CookieUtil.add(sessionRecognizeName, value, sessionMaxAge, response);
    }

}
